package com.rentcars.service.impl;

import com.rentcars.entity.Resource;
import com.rentcars.entity.RoleResource;
import com.rentcars.mapper.ResourceMapper;
import com.rentcars.mapper.RoleResourceMapper;
import com.rentcars.service.RoleResourceService;
import java.util.List;
import org.springframework.stereotype.Service;

/**
 * @author 肖克
 * @date 2021/3/3 14:40
 */

@Service
public class RoleResourceServiceImpl implements RoleResourceService {

  private final RoleResourceMapper roleResourceMapper;
  private final ResourceMapper resourceMapper;

  public RoleResourceServiceImpl(
      ResourceMapper resourceMapper, RoleResourceMapper roleResourceMapper) {
    this.resourceMapper = resourceMapper;
    this.roleResourceMapper = roleResourceMapper;
  }


  @Override
  public void addRoleSource(RoleResource roleResource) {
    roleResourceMapper.insert(roleResource);
  }

  @Override
  public void delete(int id) {
    roleResourceMapper.deleteByRoleId(id);
  }

  @Override
  public void edit(Integer roleid, String[] checkAll) {
    //删除角色所有的权限
    delete(roleid);
    for (String rid : checkAll) {
      RoleResource roleResource = new RoleResource();
      roleResource.setRoleid(String.valueOf(roleid));
      roleResource.setResourceid(rid);
      //赋予角色权限
      addRoleSource(roleResource);
    }
  }

  @Override
  public List<Resource> selectResourcesByUsername(String username) {
    return resourceMapper.getPermissionByUsername(username);
  }
}
